package com.beiding.pojo.local;


//本地化对象

import com.beiding.pojo.room.Location;
import com.beiding.pojo.room.Room;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@SolrDocument(solrCoreName = "test")
public class LocalRoom {

    public LocalRoom() {
    }

    public static List<Room> toRoomList(Iterable<LocalRoom> localRooms) {

        List<Room> rooms = new ArrayList<>();

        for (LocalRoom localRoom : localRooms) {
            rooms.add(toRoom(localRoom));
        }
        return rooms;
    }


    public static Room toRoom(LocalRoom localRoom) {

        Room room = new Room();

        if (localRoom.getImage() != null) {
            room.setImages(Sets.newHashSet(localRoom.getImage()));
        }

        Location location = new Location();

        location.setAddress(localRoom.getLocation_address());

        location.setDetailedDescription(localRoom.getLocation_detailedDescription());

        room.setLocation(location);

        room.setId(localRoom.getId());

        room.setName(localRoom.getName());

        room.setHolderId(localRoom.getHolderId());

        return room;

    }

    public static LocalRoom getInstance(Room room) {

        LocalRoom localRoom = new LocalRoom();

        localRoom.holderId = room.getHolderId();

        localRoom.id = room.getId();

        Set<String> images = room.getImages();

        if (images != null) {
            localRoom.image = images.stream().findFirst().orElse(null);
        }


        localRoom.name = room.getName();

        localRoom.location_address = room.getLocation().getAddress();

        localRoom.location_detailedDescription = room.getLocation().getDetailedDescription();

        return localRoom;
    }

    @Id
    @Indexed
    private String id;

    @Indexed
    private String name;

    @Indexed
    private String location_address;

    @Indexed
    private String location_detailedDescription;

    @Indexed
    private String image;

    @Indexed
    private String holderId;

}
